//
//  GYBannerLayout.m
//  GoodLuck
//
//  Created by apple on 2018/5/2.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYBannerLayout.h"

@interface GYBannerLayout ()

/** 存放所有cell的布局属性 */
@property (nonatomic, strong) NSMutableArray *attrsArray;

@end
@implementation GYBannerLayout

//开始布局
- (void)prepareLayout {
    [super prepareLayout];
    
    //1.首先清空属性
    [self.attrsArray removeAllObjects];
    
    // 开始创建每一个cell对应的布局属性
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    for (NSInteger i = 0; i < count; i++) {
        // 创建位置
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        // 获取indexPath位置cell对应的布局属性
        UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:indexPath];
        
        [self.attrsArray addObject:attrs];
    }

}

//返回所有布局苏属性
- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    return self.attrsArray;
}

- (nullable UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    // 创建布局属性
    UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    
    // collectionView的宽度
    //CGFloat collectionViewW = self.collectionView.width;
    
    // 设置布局属性的frame
    CGFloat w = 260;
    CGFloat h = 320;
    CGFloat x = 30;
    CGFloat y = self.collectionView.height/2-h/2;
    
    attrs.frame = CGRectMake(x*(indexPath.item+1)+indexPath.item*w, y, w, h);
    
//    //计算中心点
//    CGFloat screenCenter = self.collectionView.contentOffset.x+self.collectionView.width/2;
//    //计算 差值
//    CGFloat deltaMargin = ABS(screenCenter - attrs.center.x);
//    //计算放大比例
//    CGFloat scale = 1 - deltaMargin/(self.collectionView.width/2+attrs.size.width);
//    //设置
//    attrs.transform = CGAffineTransformMakeScale(scale, scale);
    
    
    return attrs;
}

- (CGSize)collectionViewContentSize {
    CGFloat w = 260;;
    CGFloat x = 30;
    
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    return CGSizeMake((count+1)*x+w*count, self.collectionView.height);
}

//当手指离开屏幕时会调用此方法
-(CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity{
    //取出屏幕的中心点
    CGFloat screenCenter = proposedContentOffset.x +self.collectionView.frame.size.width/2;
    //取出可见范围内的Cell
    CGRect visibleRect = CGRectZero;
    visibleRect.size = self.collectionView.frame.size;
    visibleRect.origin = proposedContentOffset;
    
    CGFloat minMargin = MAXFLOAT;
    
    for (UICollectionViewLayoutAttributes *attributes in self.attrsArray) {
        CGFloat deltaMargin = attributes.center.x -screenCenter;
        if (ABS(minMargin)>ABS(deltaMargin)) {
            minMargin = deltaMargin;
        }
    }
    return CGPointMake(proposedContentOffset.x+minMargin, proposedContentOffset.y);
}

//当屏幕的可见范围发生变化 的时候
//重新刷新视图
-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
    return YES;
}


#pragma mark - 懒加载

- (NSMutableArray *)attrsArray
{
    if (!_attrsArray) {
        _attrsArray = [NSMutableArray array];
    }
    return _attrsArray;
}


#pragma mark -- 流逝布局

//当布局刷新的时候会自动调用这个方法
//-(void)prepareLayout{
//    [super prepareLayout];
//    //修改滚动方向 为水平方向来滚动
//    self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//    //获取对应UICollectionView的Size
//    CGSize collectionSize = self.collectionView.frame.size;
//    
//    //定义显示ITEM的 宽 高
//    CGFloat itemWidth = 260;//collectionSize.height*0.6;
//    CGFloat itemHeight = 320;//collectionSize.height*0.8;
//    
//    //修改ITEM大小
//    self.itemSize = CGSizeMake(itemWidth, itemHeight);
//    
//    //设置头部和尾部的初始间距
//    CGFloat topMargin = collectionSize.width/2-itemWidth/2;
//    self.sectionInset = UIEdgeInsetsMake(0, topMargin, 0, topMargin);
//    
//}

//返回所的有的Item对应的属性设置
//- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
//    //取出所有的Item对应的属性
//    NSArray *superAttributesArray = [super layoutAttributesForElementsInRect:rect];
//    
//    //计算中心点
//    CGFloat screenCenter = self.collectionView.contentOffset.x+self.collectionView.frame.size.width/2;
//    //循环设置Item 的属性
//    
//    for (UICollectionViewLayoutAttributes  *attributes in superAttributesArray) {
//        //计算 差值
//        CGFloat deltaMargin = ABS(screenCenter - attributes.center.x);
//        //计算放大比例
//        CGFloat scale = 1 - deltaMargin/(self.collectionView.frame.size.width/2+attributes.size.width);
//        //设置
//        attributes.transform = CGAffineTransformMakeScale(scale, scale);
//    }
//    return superAttributesArray;
//}

//当手指离开屏幕时会调用此方法
//-(CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity{
//    //取出屏幕的中心点
//    CGFloat screenCenter = proposedContentOffset.x +self.collectionView.frame.size.width/2;
//    //取出可见范围内的Cell
//    CGRect visibleRect = CGRectZero;
//    visibleRect.size = self.collectionView.frame.size;
//    visibleRect.origin = proposedContentOffset;
//    
//    NSArray *visibleArray = [super layoutAttributesForElementsInRect:visibleRect];
//    
//    CGFloat minMargin = MAXFLOAT;
//    
//    for (UICollectionViewLayoutAttributes *attributes in visibleArray) {
//        CGFloat deltaMargin = attributes.center.x -screenCenter;
//        if (ABS(minMargin)>ABS(deltaMargin)) {
//            minMargin = deltaMargin;
//        }
//    }
//    return CGPointMake(proposedContentOffset.x+ minMargin, proposedContentOffset.y);
//}
//
////当屏幕的可见范围发生变化 的时候
////重新刷新视图
//-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
//    return YES;
//}

@end
